<template>
  <div class="app-container">
<!--    查询条件区域-->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="机器人名称" prop="robot_name">
        <el-input
          v-model="queryParams.robot_name"
          placeholder="请输入机器人名称"
          clearable
          @keyup.enter.native="handleQuery"
          size="small"
        />
      </el-form-item>
        <el-form-item label="参数名称" prop="param_name">
          <el-select size="small" v-model="queryParams.param_name" placeholder="请选择参数名称" clearable @change="getParamList">
            <el-option
              v-for="item in alarmParamList"
              :key="item.id"
              :label="item.param_name"
              :value="item.param_name"
            ></el-option>
          </el-select>
        </el-form-item>
      <el-form-item label="监测时间">
        <el-date-picker
          v-model="queryParams.create_times"
          style="width: 212px"
          value-format="yyyy-MM-dd"
          type="daterange"
          range-separator="-"
          start-placeholder="开始日期"
          end-placeholder="结束日期"
        ></el-date-picker>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="small" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="small" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['patrol:diagram:export']"
        >导出</el-button>
      </el-col>
    <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>
<!--  数据列表区域-->
    <el-table v-loading="loading" :data="listDiagramData" @selection-change="handleSelectionChange" :cell-style="{borderColor:'rgb(1 5 43)'}" :header-cell-style="{borderColor: 'rgb(1 5 43)'}">
      <el-table-column label="机器人名称" align="center" prop="robot_name"/>
      <el-table-column label="参数名称" align="center" prop="param_name"/>
      <el-table-column label="监测值" align="center" prop="param_value"/>
      <el-table-column label="监测时间" align="center" prop="param_time">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.param_time, '{y}-{m}-{d}')}}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-view"
            @click="handleView(scope.row)"
            v-hasPermi="['patrol:diagram:query']"
          >查看趋势图</el-button>
        </template>
      </el-table-column>
    </el-table>
<!--列表分页区域-->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList" style="background:rgb(1 5 43);"
    />
    <el-dialog :title="titlepoint" :visible.sync="open" width="1000px">
        <div id="trendContainer"  ref="trendContainer" style="height: 400px;"></div>
        <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>
<script>
import {getDetails, listDiagramData} from "@/api/ptwn/gasreport/gasreport";
import {listParamType} from "@/api/ptwn/paramtype/paramtype";
import * as echarts from 'echarts';
import "@/assets/styles/robt-bg.css";

export default {
  name: "DiagramData",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      //初始化列表数据
      listDiagramData: [],
      // 总条数
      total: 0,
      processbodyListc:[],
      //查询条件区域--初始化参数列表
      alarmParamList:[],
      // 日期范围
      // 弹出层标题
      title: "",
      titlepoint: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 8,
        robot_name: undefined,
        robot_id: undefined,
        create_times: [/*this.dealWithTime(new Date()), this.dealWithTime(new Date())*/]
      },
      queryParams1: {
        robot_config_id: undefined,
        param_name: undefined,
        param_time1: undefined
      },
      // 表单参数
      form: {},
      dialogDom:null
    };
  },
  created() {
    this.getList();    /** 初始化趋势图列表 */
    this.getParamList();//初始化参数下拉选项
  },
  methods: {
    //日期格式化
    dealWithTime(date) {
      let Y = date.getFullYear()
      let M = date.getMonth() + 1 - 0 >= 10 ? Number(date.getMonth()) + 1 : '0' + (Number(date.getMonth()) + 1)
      let D = date.getDate() > 10 ? date.getDate():"0"+date.getDate();
      return Y + '-' + M + '-' + D
    },

    /**查询条件区域---------初始化参数列表*/
    getParamList() {
      listParamType(this.queryParams).then(response => {
        this.alarmParamList = response.rows;
      });
    },
    /**初始化告警设置列表*/
    getList() {
      this.loading = true;
      listDiagramData(this.addDateRange(this.queryParams, this.dateRange)).then(response => {
        this.listDiagramData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams.robot_name=null;
      this.queryParams.robot_id=null;
      this.dateRange = [];
      this.create_times=[];
      this.queryParams.create_times=null;
      this.resetForm("queryForm");
      this.handleQuery();
      this.getParamList();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('ptwn/patrol/diagram/export', {
        ...this.queryParams
      }, `monitor_${new Date().getTime()}.xlsx`)
    },
    //点击查看趋势图详情
    handleView(row) {
      this.open = true;
      this.queryParams1.param_time1=row.param_time1;
      this.queryParams1.robot_config_id=row.robot_id;
      this.queryParams1.param_name = row.param_name;
      getDetails(this.queryParams1).then(response => {
        this.titlepoint ="参数趋势图";
        let xdata = response.data.xData;
        let ydata = response.data.yData;
        //初始化数据
        if(this.$refs.trendContainer){
          this.$nextTick(() =>{
            const myChart = echarts.init(this.$refs.trendContainer)
            this.initTrendEcharts(xdata,ydata,this.queryParams1.param_name,myChart);
          })
        }
      });
    },
    initTrendEcharts(xAxis,yAxis,param_en,myChart1) {
       setTimeout( ()=> {
        let option1;
        option1 = {
          grid:{
            left:30,
            right:30,
            top:40
          },
          tooltip: {
            trigger: 'axis'
          },
          color:['#4d96ef'],
          legend: {
            textStyle: {
              fontSize: 16,//字体大小
              color: '#00ccff'//字体颜色
            },
            data: [param_en]
          },
          xAxis: {
            type: 'category',
            axisLabel: {
              interval: 0,
              rotate: -0,
            },
            axisLine: {
              lineStyle: {
                color: '#00ccff', //x轴的颜色
              },
            },
            data: xAxis
          },
          yAxis: {
            axisTick: {
              show: true,  //刻度线
            },
            axisLabel: {
              show: true, //隐藏刻度值
            },
            type: 'value',
            splitLine:false,
            boundaryGap: [0,'100%'],
            axisLine: {
              show: true,//隐藏y轴
              lineStyle: {
                color: '#00ccff', //x轴的颜色
              },
            }
          },
          toolbox: {
            feature: {
              saveAsImage: {}
            }
          },
          series: [
            {
            symbol:'circle',//空心点改为实心点
            symbolSize:7,
            smooth: true,
            name: param_en,
            type: 'line',
            stack: 'Total',
            data: yAxis,
              markPoint: {
                data: [
                  { type: 'max', name: 'Max' },
                  { type: 'min', name: 'Min' }
                ]
              },
              markLine: {
                data: [{ type: 'average', name: 'Avg' }]
              },
            itemStyle : {
              normal:
                {
                  label :
                    {
                      show: true,
                      color:'#00ccff',
                    },
                }
            },
            lineStyle : {
              normal :{
                width:2.5,
                color:'#00ccff',
              }
            }
          }
          ],
          dataZoom: [
            {
              type: 'inside',
              startValue: 0,
              endValue: 10
            },
            {
              startValue: 0,
              endValue: 10
            }
          ]
        };
        window.addEventListener('resize', myChart1.resize);
       if (option1 && typeof option1 === 'object') {
          myChart1.setOption(option1,true);
       }
      }, 1)
    },
  }
};
</script>
